import React, { Component } from 'react';
import s from './index.module.less';
import InfoHeader from './infoHeader';
import InfoStageAdd from './infoStageAdd';
import InfoStageTab from './infoStageTab'

class ProjectInfo extends Component {

    state = {
        activeIndex: undefined,
        stages: [],
    }

    componentWillMount() {
        // 详情的时候才做这个处理
        if (!this.props.create) {
            const { stages, islawyerhost, islawyermanager } = this.props.detailData;
            this.setValue({
                activeIndex: '0', stages
            });

            // 详情的时候在这里获取所有阶段， 添加是在点击项目类型的时候
            const { getStages } = this.props.projectState;
            const { projecttype } = this.props;
            getStages({ projecttype });

            // 律师组管理员或者主办人
            this.isBoss = islawyerhost || islawyermanager;
        }
    }

    componentWillReceiveProps(nextProps) {
        // 创建任务成功更新详情
        if (this.props.create) return;
        const { stages } = this.props.detailData;
        const { stages: nextStages } = nextProps.detailData;
        const { activeIndex } = this.state;
        if (stages[activeIndex] && nextStages[activeIndex] && stages[activeIndex].tasks.length !== nextStages[activeIndex].tasks.length) {
            console.log('----inner update detail----');
            this.setState({ stages: nextStages });
        }
    }

    // 收集子组件的值
    getValue = () => {
        const { projecttype } = this.props;
        const name = this.getNameData.getValue();
        const stages = this.getStagesData.getValue();
        const data = {
            "islawyerhost": false,
            "iscomplete": false,
            "ismanager": false,
            "islawyermanager": false,
            "isclientlawyermanager": false,
            "isclientmanager": false,
            "ishost": false,
            "isclientlawyerhost": false,
            projecttype,
            name,
            stages
        }
        return data;

    }

    setValue = (currentParams, infoHeaderParams) => {
        const { stages, activeIndex, reflesh } = currentParams;
        // 请求该阶段内的基础数据
        // console.log(params);
        if ((activeIndex && activeIndex !== this.state.activeIndex && stages.length) || reflesh) {
            const {
                projectState: { getFields, getIdentity },
                projecttype
            } = this.props;
            getFields(stages[activeIndex].stageid, projecttype);
            if (projecttype === 1 || projecttype === 2) getIdentity(stages[activeIndex].stageid, projecttype);
        }

        // 再更新stage
        if (infoHeaderParams) this.getNameData.setValue(infoHeaderParams);
        this.setState(currentParams);
    }

    render() {
        const { activeIndex, stages } = this.state;
        const { userState, commonState, projectState, create, detailData, projecttype } = this.props;

        return (
            <div className={s.info}>
                <InfoHeader
                    ref={d => this.getNameData = d}
                    create={create}
                    isBoss={this.isBoss}
                    detailData={{ ...detailData, stages, projecttype }}
                    projectState={projectState}
                />
                <InfoStageAdd
                    create={create}
                    isBoss={this.isBoss}
                    detailData={{ ...detailData, stages, projecttype }}
                    projectState={projectState}
                    onStagesChange={this.setValue}
                />
                <InfoStageTab
                    ref={d => this.getStagesData = d}
                    create={create}
                    isBoss={this.isBoss}
                    activeIndex={activeIndex}
                    detailData={{ ...detailData, stages, projecttype }}
                    userState={userState}
                    commonState={commonState}
                    projectState={projectState}
                    onStagesChange={this.setValue}
                />
            </div>
        );
    }
}

export default ProjectInfo